package com.example.deekian.bluetooth_trigger.utils;

import android.content.Context;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiManager;

import com.example.deekian.bluetooth_trigger.R;
import com.example.deekian.bluetooth_trigger.model.WifiHotspotInfo;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * Created by deekian on 18-7-5.
 */

public class WifiTools {

    /**
     * 判断是否是我的热点
     * @param context
     * @return
     */
    public static boolean myWifiHotspotIsOpen(Context context) {
        WifiManager wifiManager = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
        SystemWifiHotspotInfo info = getSystemWifiHotspotInfo(wifiManager);
        return info.hotspot_state == SystemWifiHotspotInfo.WIFI_AP_STATE_ENABLED && info.providerFriendlyName.equals(context.getString(R.string.developer));
    }

    /**
     * 判断热点开启状态
     */
    public static boolean isWifiApEnabled(Context context) {
        WifiManager wifiManager = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
        return getSystemWifiHotspotInfo(wifiManager).hotspot_state == SystemWifiHotspotInfo.WIFI_AP_STATE_ENABLED;
    }

    /**
     * 开启WIFI热点
     *
     * @param context
     * @return
     */
    public static boolean openMyWifiHotspot(Context context) {
        WifiManager wifiManager = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
        Method method = null;
        try {
            //通过反射机制打开热点
            method = wifiManager.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class, boolean.class);
            WifiConfiguration netConfig = new WifiConfiguration();
            WifiHotspotInfo info = SharedPreferencesTools.getWIFIWifiHotspotInfo(context);
            netConfig.SSID = info.getHostname();
            netConfig.preSharedKey = info.getPassword();
            netConfig.providerFriendlyName = context.getString(R.string.developer);

            netConfig.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
            netConfig.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
            netConfig.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
            netConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
            netConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
            netConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
            netConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
            netConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
            method.invoke(wifiManager, netConfig, true);

        } catch (IllegalArgumentException | IllegalAccessException | SecurityException | InvocationTargetException | NoSuchMethodException e) {
            e.printStackTrace();
        }
        return isWifiApEnabled(context);
    }

    //关闭WIFI热点
    public static void cloneWifiHotspot(Context context) {
        WifiManager wifiManager = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
        SystemWifiHotspotInfo info = getSystemWifiHotspotInfo(wifiManager);
        if (info.hotspot_state == SystemWifiHotspotInfo.WIFI_AP_STATE_ENABLED || info.hotspot_state == SystemWifiHotspotInfo.WIFI_AP_STATE_ENABLING) {
            try {
                Method method = wifiManager.getClass().getMethod("getWifiApConfiguration");
                method.setAccessible(true);
                WifiConfiguration config = (WifiConfiguration) method.invoke(wifiManager);
                Method method2 = wifiManager.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class, boolean.class);
                method2.invoke(wifiManager, config, false);
            } catch (NoSuchMethodException | IllegalArgumentException | IllegalAccessException | InvocationTargetException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * @param wifiManager
     * @return true:开启 false:未开启
     */
    public static SystemWifiHotspotInfo getSystemWifiHotspotInfo(WifiManager wifiManager) {
        SystemWifiHotspotInfo info = new SystemWifiHotspotInfo();
        try {
            Method method = wifiManager.getClass().getMethod("getWifiApState");
            info.hotspot_state = (Integer) method.invoke(wifiManager);
            method = wifiManager.getClass().getDeclaredMethod("getWifiApConfiguration");
            //调用getWifiApConfiguration()方法，获取到 热点的WifiConfiguration
            WifiConfiguration configuration = (WifiConfiguration) method.invoke(wifiManager);
            info.hotspot_name = configuration.SSID;
            info.password = configuration.preSharedKey;
            info.providerFriendlyName = configuration.providerFriendlyName;

        } catch (Exception e) {
            System.out.println("Cannot get WiFi AP state" + e);
        }
        return info;
    }

}

class SystemWifiHotspotInfo {
    public static final int WIFI_AP_STATE_DISABLING = 10;   //停用中
    public static final int WIFI_AP_STATE_DISABLED = 11;    //已停用
    public static final int WIFI_AP_STATE_ENABLING = 12;    //开启中
    public static final int WIFI_AP_STATE_ENABLED = 13;     //已开启
    public static final int WIFI_AP_STATE_FAILED = 14;      //失败

    public int hotspot_state = 11;
    public String hotspot_name;
    public String password;
    public String providerFriendlyName;

    @Override
    public String toString() {
        return "hotspot_state --> " + hotspot_state + "\n" +
                "hotspot_name --> " + hotspot_name + "\n" +
                "password --> " + password + "\n" +
                "providerFriendlyName --> " + providerFriendlyName;
    }
}
